﻿#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include "debug.h"

#ifdef DEBUG_ENABLED

#include <sys/time.h>
#include <time.h>

#ifdef DEBUG_ANDROID_LOGCAT

char TEST_FILENAME[256] = "/sdcard/vaja-data/test.log";
static FILE* fd_test = NULL;
#include <android/log.h>
void setTestFilePath(char * test){
	strcpy(TEST_FILENAME,test);
}

void debug_enter(const char* text) {
	__android_log_write(ANDROID_LOG_INFO, LOG_TAG, text);
}

void debug_show(const char *format, ...) {
	va_list args;
	va_start(args, format);
	__android_log_vprint(ANDROID_LOG_DEBUG, LOG_TAG,
	                         format, args);
}
void test_init() {

	if ((fd_test = fopen(TEST_FILENAME, "a")) != NULL)
		setvbuf(fd_test, NULL, _IONBF, 0);
}
void debug_test(const char *format, ...) {
	va_list args;
	va_start(args, format);
	//  fd_log = fopen(FILENAME,"a");

#ifdef DEBUG_STDIO
		vprintf(format,args);
#else

		test_init();


	if (fd_test) {

		vfprintf(fd_test, format, args);
	    fclose(fd_test);
	}
#endif
	va_end(args);
}
void debug_time(const char* text) {
	struct timeval tv;

	gettimeofday(&tv, NULL);
	__android_log_print(ANDROID_LOG_INFO,LOG_TAG,"%03d.%03dms > %s\n",text);
}
#else


static FILE* fd_log = NULL;
static FILE* fd_test = NULL;
static const char* FILENAME = "/home/bannok/vaja-data/vaja.log";
char TEST_FILENAME[256] = "/sdcard/vaja-data/test.log";

void setTestFilePath(char * test){
	strcpy(TEST_FILENAME,test);
}

void debug_init() {
	if ((fd_log = fopen(FILENAME, "a")) != NULL)
		setvbuf(fd_log, NULL, _IONBF, 0);
}
void test_init() {

	if ((fd_test = fopen(TEST_FILENAME, "a")) != NULL)
		setvbuf(fd_test, NULL, _IONBF, 0);
}
void debug_enter(const char* text) {
	struct timeval tv;
	gettimeofday(&tv, NULL);

	//  fd_log = fopen(FILENAME,"a");
	if (!fd_log) {
		debug_init();
	}

	//if (fd_log) {
#ifdef DEBUG_STDIO
		printf("%s",text);
		//printf("\n%03d.%03dms > ENTER %s", (int) (tv.tv_sec % 1000),
		//				(int) (tv.tv_usec / 1000), text);
#else
		fprintf(fd_log, "\n%03d.%03dms > ENTER %s", (int) (tv.tv_sec % 1000),
				(int) (tv.tv_usec / 1000), text);
#endif
		//      fclose(fd_log);
	//}
}

void debug_show(const char *format, ...) {
	va_list args;
	va_start(args, format);
	//  fd_log = fopen(FILENAME,"a");

#ifdef DEBUG_STDIO
		vprintf(format,args);
#else
	if (!fd_log) {
		debug_init();
	}

	if (fd_log) {

		vfprintf(fd_log, format, args);
	//      fclose(fd_log);
	}
#endif
	va_end(args);
}
void debug_test(const char *format, ...) {
	va_list args;
	va_start(args, format);
	//  fd_log = fopen(FILENAME,"a");

#ifdef DEBUG_STDIO
		vprintf(format,args);
#else

		test_init();


	if (fd_test) {

		vfprintf(fd_test, format, args);
	    fclose(fd_test);
	}
#endif
	va_end(args);
}

void debug_time(const char* text) {
	struct timeval tv;

	gettimeofday(&tv, NULL);
#ifdef DEBUG_STDIO
	printf("%03d.%03dms > %s\n", (int) (tv.tv_sec % 1000),
					(int) (tv.tv_usec / 1000), text);
#else
	//  fd_log = fopen(FILENAME,"a");
	if (!fd_test) {
		test_init();
	}
	if (fd_test) {
		fprintf(fd_log, "\n%03d.%03dms > %s", (int) (tv.tv_sec % 1000),
				(int) (tv.tv_usec / 1000), text);
		//      fclose(fd_log);
	}
#endif
}


#endif

#endif

